package com.csot.job;

import com.csot.model.*;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.List;

public class CityCheckIdleIp {

    public void checkCityIp() {

        List<Record> rList = Db.find(" select ip_address, ip_city,COUNT(*) as count_num from " +
                "( select tci.ip_address,tci.ip_city from t_city_ip tci union all select tci2.ip_address,tci2.ip_city " +
                " from t_coarse_ip tci2 )" +
                " as combined_table group by ip_address,ip_city having COUNT(*) > 2");

        List<CityidleIp> cityIdleIps = new ArrayList<>();
        for (Record record : rList) {
            CityidleIp cityIdleIp = new CityidleIp();
            cityIdleIp.setIpAddress(record.getStr("ip_address"));
            cityIdleIp.setIpCity(record.getStr("ip_city"));
            cityIdleIps.add(cityIdleIp);
        }

        Db.tx(() -> {
            Db.delete("delete from t_cityidle_ip");
            Db.batchSave(cityIdleIps, 999);
            return true;
        });

    }


}
